package com.youdelu.road.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.youdelu.road.R;
import com.youdelu.road.bean.Attr;
import com.youdelu.road.bean.BgAttr;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JTDListAdapter extends BaseAdapter{
    private LayoutInflater inflater;
    private ClickCallBack clickCallBack;
    private Context context=null;
    public List<Attr> dirs = null;

    private int index = -1;

    private class Holder{
        public TextView getTitle() {
            return title;
        }

        public void setTitle(TextView title) {
            this.title = title;
        }

        public RadioGroup getRadio() {
            return radio;
        }

        public void setRadio(RadioGroup radio) {
            this.radio = radio;
        }

        TextView title;
        RadioGroup radio ;
        RadioButton radio1,radio2,radio3,radio4,radio5,radio6,radio7,radio8,radio9,radio10,radio11;
        LinearLayout xs_l,qbb_l;
        public EditText getXs() {
            return xs;
        }

        public void setXs(EditText xs) {
            this.xs = xs;
        }

        EditText xs,qbb;
    }
    //构造方法
    public JTDListAdapter(Context context, ClickCallBack clickCallBack, List<Attr> dirs) {
        this.inflater= LayoutInflater.from(context);
        this.context = context;
        this.clickCallBack = clickCallBack;
        if(dirs==null){
            dirs = new ArrayList<>();
        }
        this.dirs =  dirs;
    }


    public void setIndex(int index){
        this.index = index ;
    }
    public void update(List<Attr> dirs){
        if(dirs==null){
            dirs = new ArrayList<>();
        }
        this.dirs =  dirs;
        this.notifyDataSetChanged();
    }



    public Attr getAttr(int position){
        return dirs.get(position);
    }

    @Override
    public int getCount() {
        return dirs.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        Holder holder;
        if(view==null){
            view= inflater.inflate(R.layout.view_jtdlist_item,null);
            holder=new Holder();
            holder.title= view.findViewById(R.id.title);
            holder.radio = view.findViewById(R.id.radio);
            holder.radio1 = view.findViewById(R.id.radio1);
            holder.radio2 = view.findViewById(R.id.radio2);
            holder.radio3 = view.findViewById(R.id.radio3);
            holder.radio4 = view.findViewById(R.id.radio4);
            holder.radio5 = view.findViewById(R.id.radio5);
            holder.radio6 = view.findViewById(R.id.radio6);
            holder.radio7 = view.findViewById(R.id.radio7);
            holder.radio8 = view.findViewById(R.id.radio8);
            holder.radio9 = view.findViewById(R.id.radio9);
            holder.radio10 = view.findViewById(R.id.radio10);
            holder.radio11 = view.findViewById(R.id.radio11);
            holder.xs = view.findViewById(R.id.xs);
            holder.qbb = view.findViewById(R.id.qbb);
            holder.xs_l = view.findViewById(R.id.xs_l);
            holder.qbb_l = view.findViewById(R.id.qbb_l);
            view.setTag(R.id.tag,holder);
        }else{
            holder=(Holder) view.getTag(R.id.tag);
        }
        holder.xs.setTag(R.id.tag_first,position);
        holder.xs.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    int position = (Integer) v.getTag(R.id.tag_first);
                    if(dirs.size()<position){
                        return;
                    }
                        EditText editText = (EditText)v;
                        String t = editText.getText().toString();
                        String regEx="[^0-9]";
                        Pattern p = Pattern.compile(regEx);
                        Matcher m = p.matcher(t);
                        t= m.replaceAll("").trim();
                        if(!"".equals(t)){
                            int xs = Integer.valueOf(t);
                            dirs.get(position).setPath(String.valueOf(xs));
                            clickCallBack.select(position);
                        }
                }
            }
        });
        holder.qbb.setTag(R.id.tag_first,position);
        holder.qbb.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    if(dirs.size()<position){
                        return;
                    }
                    int position = (Integer) v.getTag(R.id.tag_first);
                    EditText editText = (EditText)v;
                    String t = editText.getText().toString();
                    dirs.get(position).setPath(t);
                    clickCallBack.select(position);
                }
            }
        });
        hideAll(holder);
        int type = dirs.get(position).getType();
        int filp = dirs.get(position).getFilp();
        float rotation = dirs.get(position).getRotation();
        int pp = dirs.get(position).getPosition();
        if(type==0){
            if(pp==1){
                showJtd(holder);
            }else if(pp==2){
                showJtd1(holder);
            }
        }else if(type==1){
            if(pp==1){
                showQbb(holder,filp,rotation);
            }else if(pp==2){
                showQbb1(holder,filp,rotation);
            }
        }else if(type==2){
            showHld(holder);
        }else if(type==3){
            showQbbText(holder);
        }else if(type==4){
            showBdd(holder);
        }

        view.setTag(R.id.tag_first,dirs.get(position));
        String t = dirs.get(position).getTitle() ;
        if(index==position){
            t = t+" <- 选择";
        }
        holder.title.setText(t);
        if(index==position){
            holder.title.setTextColor(Color.RED);
        }else{
            holder.title.setTextColor(Color.WHITE);
        }
        int state = dirs.get(position).getState();
        if(state==1){
            holder.radio1.setChecked(true);
        }else if(state==2){
            holder.radio2.setChecked(true);
        }else if(state==3){
            holder.radio3.setChecked(true);
        }else if(state==4){
            holder.radio4.setChecked(true);
        }else if(state==5){
            holder.radio5.setChecked(true);
        }else if(state==6){
            holder.radio6.setChecked(true);
        }else if(state==7){
            holder.radio7.setChecked(true);
        }else if(state==8){
            holder.radio8.setChecked(true);
        }else if(state==9){
            holder.radio9.setChecked(true);
        }else if(state==10){
            holder.radio10.setChecked(true);
        }
        String path = dirs.get(position).getPath();
        if(path!=null){
            holder.xs.setText(path);
            holder.qbb.setText(path);
        }
        holder.radio.setTag(R.id.tag_first,dirs.get(position).getId());
        holder.radio.setTag(R.id.tag_second,position);
        holder.radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                View viewById = group.findViewById(checkedId);
                if (!viewById.isPressed()){
                    return;
                }
                int i = 0 ;
                switch (group.getCheckedRadioButtonId()){
                    case R.id.radio1:
                        i = 1 ;
                        break;
                    case R.id.radio2:
                        i = 2 ;
                        break;
                    case R.id.radio3:
                        i = 3 ;
                        break;
                    case R.id.radio4:
                        i = 4 ;
                        break;
                    case R.id.radio5:
                        i = 5 ;
                        break;
                    case R.id.radio6:
                        i = 6 ;
                        break;
                    case R.id.radio7:
                        i = 7;
                        break;
                    case R.id.radio8:
                        i = 8 ;
                        break;
                    case R.id.radio9:
                        i = 9 ;
                        break;
                    case R.id.radio10:
                        i = 10 ;
                        break;
                    case R.id.radio11:
                        i = 20;
                        break;
                }
                if(i==7){
                    i = 9 ;
                }else if(i==8){
                    i = 10 ;
                }else if(i==9){
                    i=11;
                }else if(i==10){
                    i=12;
                }

                int position =(Integer)group.getTag(R.id.tag_second);

                int type = getAttr(position).getFilp();
                float tota = getAttr(position).getRotation();
                if((type==0&&tota==1)||(type==1&&tota==0)){
                    if(i==5){
                        i = 7 ;
                    }else if(i==6){
                        i = 8 ;
                    }
                }

                if(i==20){
                    i = dirs.get(position).getDefault_value();
                }
                dirs.get(position).setState(i);
                clickCallBack.select(position);
            }
        });
        return view;
    }
    public void hideAll(Holder holder){
        holder.radio1.setVisibility(View.GONE);
        holder.radio2.setVisibility(View.GONE);
        holder.radio3.setVisibility(View.GONE);
        holder.radio4.setVisibility(View.GONE);
        holder.radio5.setVisibility(View.GONE);
        holder.radio6.setVisibility(View.GONE);
        holder.radio7.setVisibility(View.GONE);
        holder.radio8.setVisibility(View.GONE);
        holder.radio9.setVisibility(View.GONE);
        holder.radio10.setVisibility(View.GONE);
        holder.radio11.setVisibility(View.GONE);
        holder.xs_l.setVisibility(View.GONE);
        holder.qbb_l.setVisibility(View.GONE);
    }
    public void showJtd(Holder holder){
        holder.radio1.setVisibility(View.VISIBLE);
        holder.radio3.setVisibility(View.VISIBLE);
        holder.radio5.setVisibility(View.VISIBLE);
        holder.radio6.setVisibility(View.VISIBLE);
    }
    public void showJtd1(Holder holder){
        holder.radio1.setVisibility(View.VISIBLE);
        holder.radio3.setVisibility(View.VISIBLE);
        holder.radio5.setVisibility(View.VISIBLE);
        holder.radio6.setVisibility(View.VISIBLE);
    }
    public void showQbb(Holder holder,int zy,float qh){
        if((zy==0&&qh==0)||(zy==1&&qh==0)){
            holder.radio4.setVisibility(View.VISIBLE);
            holder.xs_l.setVisibility(View.VISIBLE);
        }else{
            holder.radio1.setVisibility(View.VISIBLE);
            holder.radio3.setVisibility(View.VISIBLE);
        }
    }
    public void showQbb1(Holder holder,int zy,float qh){
        if((zy==0&&qh==0)||(zy==1&&qh==0)){
            holder.radio4.setVisibility(View.VISIBLE);
            holder.xs_l.setVisibility(View.VISIBLE);
        }else{
            holder.radio1.setVisibility(View.VISIBLE);
            holder.radio3.setVisibility(View.VISIBLE);
        }
    }
    public void showHld(Holder holder){
        holder.radio7.setVisibility(View.VISIBLE);
        holder.radio8.setVisibility(View.VISIBLE);
        holder.radio9.setVisibility(View.VISIBLE);
        holder.radio10.setVisibility(View.VISIBLE);
    }
    public void showQbbText(Holder holder){
        holder.qbb_l.setVisibility(View.VISIBLE);
    }
    public void showBdd(Holder holder){
        holder.radio3.setVisibility(View.VISIBLE);
        holder.radio11.setVisibility(View.VISIBLE);
    }
    public interface ClickCallBack {
        void select(int position);
    }
    class MyTextWatcher implements TextWatcher {
        public MyTextWatcher(EditText holder) {
            mHolder = holder;
        }

        private  EditText mHolder;

        @Override
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s != null && !"".equals(s.toString())) {
                int type = (Integer) mHolder.getTag(R.id.tag_second);
                int position = (Integer) mHolder.getTag(R.id.tag_first);
                if(type==0){
                    String t = s.toString();
                    String regEx="[^0-9]";
                    Pattern p = Pattern.compile(regEx);
                    Matcher m = p.matcher(t);
                    t= m.replaceAll("").trim();
                    if(!"".equals(t)){
                        int xs = Integer.valueOf(t);
                        dirs.get(position).setPath(String.valueOf(xs));
                    }
                }else{
                    dirs.get(position).setPath(s.toString());
                }
                clickCallBack.select(position);
            }
        }
    }
}